const readline = require('readline-sync');

// 管理员
const users = [{
        username: 'zhangsan',
        password: '123'
    },
    {
        username: 'lisi',
        password: '456'
    }
];
// 学生
const studentsData = [{
        id: 1,
        name: 'zhao',
        age: '18',
        gender: 'male',
        score: '98'
    },
    {
        id: 2,
        name: 'qian',
        age: '20',
        gender: 'female',
        score: '100'
    },
    {
        id: 3,
        name: 'sun',
        age: '17',
        gender: 'male',
        score: '77'
    },
    {
        id: 4,
        name: 'li',
        age: '22',
        gender: 'female',
        score: '65'
    },
    {
        id: 5,
        name: 'zhou',
        age: '19',
        gender: 'male',
        score: '80'
    }
];
/*
需求：
- 管理员信息功能：
* 可登录、注册
* 登录成功后可对学生信息进行操作

- 学生信息功能：
* 操作提示： 1.新增  2.删除  3.查询  4.修改  5.退出
* 新增可增： name、age、gender、score。 id 自增
* 删除： 按 id 删
* 查询：1.查询所有 2.分类查询 3.退出查询
     1. 打印所有学生数组
     2. 1.姓名 2.年龄 3.性别 4.成绩
* 修改：根据输入的学生 id 修改，可改：
     1.姓名 2.年龄 3.性别 4.成绩
*/





function main() {
    let user = null;
    let Login_loop = true;
    while (Login_loop) {
        console.log('请选择1. 登录 2. 注册');
        let opt = Number(readline.question());
        switch (opt) {
            case 1:
                user = login();
                Login_loop = false;
                break;
            case 2:
                register();
                break;

        }
    }
    if (user) {
        console.log('学生管理系统');
        let cycle = true;
        while (cycle) {
            console.log(' 1.新增  2.删除  3.查询  4.修改  5.退出');
            let opt = readline.question() - 0;
            switch (opt) {
                case 1:
                    add();
                    break;
                case 2:
                    destroy();
                    break;
                case 3:
                    lnquire();
                    break;
                case 4:
                    revise();
                    break;
                case 5:
                    console.log('886');
                    cycle = false;
                    break;

            }
        }
    }
}

main();




function login() {
    console.log('请输入账号');
    let username = readline.question();
    console.log('请输入密码');
    let password = readline.question();
    let result = users.filter(item => item.username == username && item.password == password)
    if (result.length) {
        console.log('登录成功');

        return result;
    } else {
        console.log('查无此人');
    }
}

function register() {
    console.log('请输入注册账号');
    let username = readline.question();
    let result = users.some(item => item.username == username)
    if (result) {
        console.log('账号已被注册');
    } else {
        console.log('请输入注册密码');
        let password = readline.question();
        users.push({
            username,
            password
        })
        console.log(users);
        console.log('注册成功');
    }
}


//新增学生
function add() {
    console.log('请输入学生名称');
    let name = readline.question();
    console.log('请输入年纪');
    let age = readline.question();
    console.log('请选择性别 1.男性 2.女性');
    let gender = readline.question();
    switch (gender) {
        case 1:
            gender = 'male';
            break;
        case 2:
            gender = 'female';
            break;

    }
    console.log('请输入成绩');
    let score = readline.question();
    studentsData.push({
        id: studentsData.length + 1,
        name,
        age,
        gender,
        score
    })
    console.log(studentsData);
}

//删除学生
function destroy() {
    console.log('请输入删除学生ID');
    let id = readline.question();
    let result = studentsData.filter(item => item.id == id);
    if (result.length) {
        delete studentsData[id - 1];
    } else {
        console.log('查无此人');
    }
    console.log(studentsData);
}


//查询学生
function lnquire() {
    console.log('1.查询所有 2.分类查询 3.退出查询');
    let lnquire = readline.question() - 0;
    switch (lnquire) {
        case 1:
            console.log(studentsData);
            break;
        case 2:
            console.log(' 1.姓名 2.年龄 3.性别 4.成绩');
            let opt = readline.question() - 0;
            console.log('请输入查询内容');
            let cont = readline.question();
            switch (opt) {
                case 1:
                    opt = 'name';
                    break;
                case 2:
                    opt = 'age';
                    break;
                case 3:
                    console.log('请选择性别1.男性 2.女性');
                    let gender = readline.question() - 0;
                    switch (gender) {
                        case 1:
                            opt = 'gender';
                            break;
                        case 2:
                            opt = 'gender';
                            break;
                    }

                    break;
                case 4:
                    opt = 'score';
                    break;
            }
            let result = studentsData.filter(item => item[opt] == cont);
            console.log(result);
            break;
        case 3:
            console.log('进来干什么？');
            break;
    }
}


//修改信息
function revise() {
    console.log('请选择修改学生ID');
    let id = readline.question() ;
    let result = studentsData.filter(item => item.id == id)[0];
    console.log(result);
    // let modify_loop = true;
    console.log('请选择修改内容 1.姓名 2.年龄 3.性别 4.成绩 5.退出');
    let opt = readline.question() - 0;
    // let result =studentsData.filter(item =>item.id == id );
    // console.log(result);
    switch (opt) {
        case 1:
            opt = 'name'
            break;
        case 2:
            opt = 'age'
            break;
        case 3:
            console.log('请选择修改性别 1.男性 2.女性');
            let gender = readline.question() - 0;
            switch (gender) {
                case 1:
                    opt = 'gender'
                    break;
                case 2:
                    opt = 'gender'
                    break;
            }
            break;
        case 4:
            opt = 'score';
            break;
     
    }
    console.log('请输入修改内容');
    let cont = readline.question();
    result[opt] =cont;
    console.log(result);

}